using System.Drawing;
using System.Windows.Forms;
using CloudShot.Editor.CanvasFigures;
using CloudShot.Entities;
using CloudShot.Utils;

namespace CloudShot.Editor.Tools.States
{
  internal class NetSelectionState : IPointerToolState
  {
    private readonly IDrawingCanvas _canvas;
    private readonly Point _startPoint;
    private readonly FocusRectangle _focusRectangle = new FocusRectangle();

    public NetSelectionState(IDrawingCanvas canvas, Point startPoint)
    {
      _canvas = canvas;
      _startPoint = startPoint;

      // click on background
      if (!_canvas.IsAnyModifierPressed(KeyModifiers.Ctrl))
        _canvas.GraphicsList.DeselectAll();

      _focusRectangle.Rectangle = RectangleUtils.GetNormalizedRectangle(_startPoint, _startPoint);
      _canvas.GraphicsList.Add(_focusRectangle);
    }

    #region IPointerToolState Members

    public void OnKeyDown(KeyEventArgs keyEventArgs)
    {
      //Empty
    }

    public void OnKeyUp(KeyEventArgs keyEventArgs)
    {
      //Empty
    }

    public void Enter() {}

    public void OnMouseMove(Point point)
    {
      using (new AutomaticCanvasRefresher(_canvas, () => _focusRectangle.GetBoundingBox()))
      {
        _focusRectangle.Rectangle = RectangleUtils.GetNormalizedRectangle(_startPoint, point);
      }
    }

    public void Release()
    {
      // Group selection
      _canvas.GraphicsList.SelectInRectangle(_focusRectangle.Rectangle);
      _canvas.GraphicsList.Remove(_focusRectangle);
    }

    #endregion
  }
}